<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: daisc
 * QQ: 2444329889
 * Time: 2018/01/10 15:16
 * Desc: desc
 */
class Model_Supplier_Brokerage extends ORM
{


    /**
     * @function 添加佣金记录
     */
    public static function add_brokerage($ordersn,$order=null)
    {
        if(!$ordersn)
        {
            return false;
        }
        if(!$order['status']||!$order['supplierlist'])
        {
            $order = DB::select('id','status','supplierlist')->from('member_order')
                ->where('ordersn','=',$ordersn)->execute()->current();
        }
        //必须是已经付款的订单,且有供应商
        if(!in_array($order['status'],array(2,5))||!$order['supplierlist'])
        {
            return false;
        }
        $order_compute = DB::select()->from('member_order_compute')
            ->where('order_id','=',$order['id'])->execute()->current();
        //检测是否是定金支付
        if(!((float)$order_compute['subscription_price']))
        {
            $brokerage = $order_compute['supplier_commission'];
            $total_price = $order_compute['total_price'];
        }
        else
        {
            $brokerage = $order_compute['supplier_commission'];
            $total_price = $order_compute['subscription_price'];
        }

        $brokerage < 0 ? $brokerage = 0 :'';
        $data = array(
            'supplierlist'=>$order['supplierlist'],
            'ordersn'=>$ordersn,
            'addtime'=>time(),
            'order_price'=>$total_price,
            'brokerage'=>$brokerage
        );
        try
        {
            DB::insert('supplier_brokerage',array_keys($data))
                ->values(array_values($data))->execute();
            return true;
        }catch (Exception $e)
        {
            return false;
        }

    }


    /**
     * @function 自动更新结算状态
     */
    public static function auto_change_status()
    {
        $config = Model_Sysconfig::get_configs(0,array('cfg_supplier_brokerage_start_days','cfg_supplier_brokerage_finish_days','cfg_supplier_brokerage_type'));

        $days = $config['cfg_supplier_brokerage_finish_days'];
        $time = empty($days) ?  time() : strtotime("-$days days");
        $sql ="select a.id,b.ordersn,b.status,b.id as orderid from sline_supplier_brokerage as a LEFT JOIN sline_member_order as b on a.ordersn=b.ordersn WHERE a.status=1 and (b.status=5 or b.status=4) and (b.finishtime<=$time OR  b.`finishtime` IS NULL)";

        $list = DB::query(1,$sql)->execute()->as_array();
        $ids = array();
        $refund_ids = array();
        foreach ($list as $l)
        {
            if($l['id'] && $l['status']!=4)
            {
                $ids[] = $l['id'];
            }
            else if($l['id'] && $l['status']==4)
            {
                $refund_ids[] = $l['id'];
            }

        }
        if($ids)
        {
            $ids = implode(',',$ids);
            $data = array(
                'finish_brokerage'=>DB::expr('brokerage'),
                'status'=>2,
                'modtime'=>time(),
            );
            DB::update('supplier_brokerage')->set($data)->where("id in ($ids)")->execute();
        }

        if($refund_ids)
        {
            $refund_ids_str = implode(',',$refund_ids);
            $data = array(
                'finish_brokerage'=>DB::expr('refund_cost'),
                'status'=>2,
                'modtime'=>time(),
            );
            DB::update('supplier_brokerage')->set($data)->where("id in ($refund_ids_str)")->execute();
        }
        return true;
    }


    /**
     * @function 获取供应商的金额情况
     */
    public static function get_supplier_price_info($supplierid)
    {
        if(!$supplierid)
        {
            return array();
        }
        $withdraw_price_finish = self::get_supplier_withdraw_price($supplierid,1);//已提现的金额
        $withdraw_price_approval = self::get_supplier_withdraw_price($supplierid,0);//申请中的金额
        $brokerage_price = self::get_supplier_brokerage_price($supplierid);
        return array(
            'allow_price'=>$brokerage_price-$withdraw_price_finish-$withdraw_price_approval,//可提现金额
            'withdraw_price_finish'=>$withdraw_price_finish ? $withdraw_price_finish : 0,//已提现金额
            'withdraw_price_approval'=>$withdraw_price_approval ? $withdraw_price_approval : 0,//申请中的金额
            'brokerage_price'=>$brokerage_price,//佣金
        );

    }

    /**
     * @function 获取供应商提现金额
     * @param $supplierid
     * @param $status 提现状态
     */
    public static function get_supplier_withdraw_price($supplierid,$status)
    {
        return DB::select(DB::expr('sum(withdrawamount) as price'))
            ->from('supplier_finance_drawcash')
            ->where('supplierid','=',$supplierid)
            ->and_where('status','=',$status)
            ->execute()->get('price');

    }


    /**
     * @function 获取供应商佣金金额
     * @param $supplierid
     */
    public static function get_supplier_brokerage_price($supplierid)
    {
        return DB::query(Database::SELECT,"select sum(a.brokerage) as total from sline_supplier_brokerage a left join sline_member_order b on a.ordersn=b.ordersn where a.supplierlist='".$supplierid."' and b.status in(2,5) and a.status=2 ")->execute()->get('total');

    }

    /**
     * @function 获取提现方式的中文名称
     * @param $type
     */
    public static function get_proceeds_type($type)
    {
        $arr = array(
            '1'=>'银行',
            '2'=>'支付宝',
            '3'=>'微信',
        );
        return $arr[$type];
    }

    /**
     * @function 获取所有供应商的财务信息
     */
    public static function get_total_price_info()
    {
        //总佣金
        $total_price = DB::select(DB::expr('sum(finish_brokerage) as total'))
            ->from('supplier_brokerage')->execute()->get('total');
        //已经完成提现的
        $withdraw_price_finish = DB::select(DB::expr('sum(withdrawamount) as price'))
            ->from('supplier_finance_drawcash')
            ->and_where('status','=',1)
            ->execute()->get('price');
        //申请中的
        $withdraw_price_approval = DB::select(DB::expr('sum(withdrawamount) as price'))
            ->from('supplier_finance_drawcash')
            ->and_where('status','=',0)
            ->execute()->get('price');
        return array(
            'allow_price'=>number_format($total_price-$withdraw_price_finish-$withdraw_price_approval,2) ,//可提现金额
            'withdraw_price_finish'=>number_format($withdraw_price_finish ? $withdraw_price_finish : 0,2),//已提现金额
            'withdraw_price_approval'=>number_format($withdraw_price_approval ? $withdraw_price_approval : 0,2),//申请中的金额
        );

    }


    /**
     * @function 获取供应商时间范围内的财务信息
     * @param $supplierid
     * @param $starttime
     * @param $endtime
     */
    public static function get_supplier_time_price($supplierid,$starttime,$endtime)
    {
        if($endtime)
        {
            $endtime = $endtime+24*60*60;
        }
        //佣金总
        $w = "supplierlist=$supplierid";
        if($starttime)
        {
            $w .=" and addtime>=$starttime";
        }
        if($endtime)
        {
            $w .= " and addtime<=$endtime";
        }
        $total_price = DB::select(DB::expr('sum(finish_brokerage) as total'))->from('supplier_brokerage')
            ->where($w)->execute()->get('total');
        //提现记录信息
        $w = "supplierid=$supplierid";
        if($starttime)
        {
            $w .=" and addtime>=$starttime";
        }
        if($endtime)
        {
            $w .= " and addtime<=$endtime";
        }
        //提现中金额
        $withdraw_price_approval = DB::select(DB::expr('sum(withdrawamount) as total'))->from('supplier_finance_drawcash')
            ->where($w)->and_where('status','=',0)->execute()->get('total');
        //已提现金额
        $withdraw_price_finish = DB::select(DB::expr('sum(withdrawamount) as total'))->from('supplier_finance_drawcash')
            ->where($w)->and_where('status','=',1)->execute()->get('total');
        return array(
            'allow_price'=>number_format($total_price-$withdraw_price_finish-$withdraw_price_approval,2) ,//可提现金额
            'withdraw_price_finish'=>number_format($withdraw_price_finish ? $withdraw_price_finish : 0,2),//已提现金额
            'withdraw_price_approval'=>number_format($withdraw_price_approval ? $withdraw_price_approval : 0,2),//申请中的金额
        );

    }

    /**
     * @function 移除供应商财务信息
     * @param $supplierid
     * @param $starttime
     * @param $endtime
     */
    public static function refund_brokerage($org_status,$order)
    {
        if ($org_status == $order['status'])
            return true;
        $ordersn=$order['ordersn'];
        if(!in_array($org_status,array(2,6))||!$order['supplierlist']||!$ordersn)
        {
            return true;
        }
        //退款
        if ($order['status'] == 4)
        {
            $old_data = DB::select()
                ->from('supplier_brokerage')
                ->where('ordersn', '=', $ordersn)
                ->execute()->current();

           DB::delete('supplier_brokerage_refund')->where('ordersn','=',$old_data['ordersn'])->execute();

            $flag = DB::insert('supplier_brokerage_refund', array_keys($old_data))
                ->values(array_values($old_data))->execute();

            if ($flag) {
              //  $status = DB::delete('supplier_brokerage')->where('ordersn', '=', $ordersn)->execute();
               // return $status;
            }
            return $flag;
        }
        return false;
    }
    //更新退款手续费
    public static function update_refund_cost($org_status,$order,$refund_cost)
    {

        if ($org_status == $order['status'])
            return true;
        $ordersn=$order['ordersn'];
        if(!in_array($org_status,array(2,6))||!$order['supplierlist']||!$ordersn)
        {
            return true;
        }
        //退款
        if ($order['status'] == 4)
        {
            DB::query(Database::UPDATE,"update sline_supplier_brokerage set refund_cost='{$refund_cost}' where ordersn='{$ordersn}' ")->execute();
        }

    }

    public static function get_channel_commission($supplier_id)
    {
        $channel = Model_Supplier::get_ota_channel($supplier_id,true);
        foreach ($channel as &$v){
            $v['commission'] = self::get_channel_order($v['id'], $supplier_id);
        }

        return $channel;
    }

    public static function get_channel_order($channel_id, $supplier_id)
    {
        $order = DB::select('id','ordersn','status')->from('member_order')
            ->where("supplierlist={$supplier_id} and channel_id={$channel_id} and status IN(2,5)")
            ->execute()->as_array();

        $settled_sn = array();
        $unsettled_sn = array();
        foreach ($order as $v){
            if($v['status']==5){
                array_push($settled_sn,$v['ordersn']);
            }else{
                array_push($unsettled_sn,$v['ordersn']);
            }
        }
        $settled_sn = implode(',',$settled_sn);
        $unsettled_sn = implode(',',$unsettled_sn);

        $settled_commission = 0;
        $unsettled_commission = 0;
        if($settled_sn){
            $settled_commission_list = DB::select()->from('supplier_brokerage')->where("ordersn IN({$settled_sn})")->execute()->as_array();
            $settled_commission = array_sum(array_column($settled_commission_list,'finish_brokerage'));
        }
        if($unsettled_sn){
            $unsettled_commission_list = DB::select()->from('supplier_brokerage')->where("ordersn IN({$unsettled_sn})")->execute()->as_array();
            $unsettled_commission = array_sum(array_column($unsettled_commission_list,'brokerage'));
        }

        $data = array(
            'settled'     => $settled_commission,
            'unsettled'   => $unsettled_commission,
            'total_price' => bcadd($settled_commission,$unsettled_commission,2)
        );
        return $data;
    }
}